define (require, exports, module) ->
  # 引入样式
  require 'mods/prod_search/prod_search_css.css'
  # 引入API服务
  search_api = require 'mods/prod_search/prod_search_api'
  bus = require 'mods/bus'

  module.exports = Vue.extend

    template: require 'mods/prod_search/prod_search_tpl.js'

    data: () ->
      # 是否加载完成
      loadedSucc: false
      # 加载失败提示语
      loadedFail: ''
      # 当前页
      page: 1
      # 搜索结果是否为空
      dataIsEmpty: true
      # 查看更多
      viewMoreTxt: '查看更多'
      # 搜索结果列表
      searchItems: []
      # 搜索字段
      q: ''

    # 方法
    methods:

      # 插件加载
      pluginLoad: () ->
        self = @

      # 加载成功
      loadedSuccFunc: () ->
        self = @
        self.$nextTick () ->
          # 加载成功标识
          self.loadedSucc = true
          # 插件加载
          self.pluginLoad()

      # 分页
      gotoPage: (page) ->
        self = @
        self.page = parseInt page
        self.getSearchList()

      # 公告详情页跳转
      goNoticeDetail: (detail_id) ->
        self = @
        self.$router.push {name:'notice_detail', params: {'type': 'gg', 'detail_id': detail_id} }

      # 法律文件详情页跳转
      goLawDetail: (detail_id) ->
        self = @
        self.$router.push {name:'notice_detail', params: {'type': 'law', 'detail_id': detail_id} }

      # 搜索框键盘弹起触发事件
      getSearchList: () ->
        self = @

        params =
          q: self.q,
          pageNum: self.page

        return search_api.getSearchList params
          .then (res) ->

            tmpData = res?.data?.data
            
            if self.searchItems.length == 0
              if tmpData?.length != 0
                self.searchItems = tmpData
                self.dataIsEmpty = false
              else
                self.dataIsEmpty = true
            else
              # 隐藏"暂无数据"提示
              self.dataIsEmpty = false

              if tmpData?.length != 0
                for item in tmpData
                  self.searchItems.push item
              else
                self.viewMoreTxt ='已显示全部内容'

            self.page = res?.body?.pageNum

            # 加载成功
            self.loadedSuccFunc()

          .catch (err) ->
            console.error 'err:',err

      # 查看搜索信息详情
      onSearchDetail: (url) ->
        self = @
        self.$router.push {path: url}

    watch:
      '$router.params': 'getSearchList'

    # 计算属性
    computed:
      # 加载中的样式
      loaddingClass: () ->
        self = @
        if self.loadedSucc
          return 'c-hidden'
        if self.loadedFail
          return 'c-hidden'
        return ''

      # 加载成功的样式
      loadedSuccClass: () ->
        self = @
        return if self.loadedSucc then '' else 'c-vhidden'

      # 加载失败的样式
      loadedFailClass: () ->
        self = @
        return if self.loadedFail then '' else 'c-hidden'

    created: () ->
      self = @
      bus.$on 'getSearchList', (q) ->
        self.searchItems = []
        self.page =  1
        self.q = q
        self.gotoPage self.page

    # 初始化
    mounted: () ->
      self = @
      self.q = self.$route.params?.q
      self.gotoPage self.page